//彩宝筛选
var type;
var PlaceOrder=false;//代下单
(function(){
	var gemsTerm={
		init:function(){
			args = GetUrlParms();				
			if(args["team"]!=undefined){						
				 TEAM = args["team"];//2是现货					
			} 
			if(args["cid"]!=undefined){						
				 Customer_id = args["cid"];	
				 PlaceOrder=true;
			}
			this.commonParamers();
			this.bindEvent();
			//this.loadSupplier();
		},
		bindEvent:function(){
			var self=this;
			if(TEAM==2){
				$(".team_params").show();
				$(".normal_params").hide();
			}else{
				$(".team_params").hide();
				$(".normal_params").show();
			}
			//切换彩宝类型
			$(".gems_type span").on("click",function(){
				$(this).toggleClass("active").siblings().removeClass("active");
				type=$(".gems_type span.active").attr("data-type");
				self.changType(type);				
			});
			//点击 选中                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
			$("#term_page").on("click",".term span",function(){
				if($(this).parent().hasClass("weight")){
					$(this).toggleClass("active").siblings().removeClass("active");
					 self.getWeight($(this));
				}else{
					$(this).toggleClass("active");
				}
			});
			//点击搜索
			$("#filter_product_btn").on("click",function(){
				self.getQueryParameters();				
			});
			//筛选重置
            $(".reset_term_btn").on("click", function () {
                 $(".term span").removeClass("active");//清除其他条件选中的样式
               $(".term-box input").val("");//清除input框里的值
            });
			 //手动输入重量时移出选中的重量
            $(".weight-input input").keypress(function () {
                $(".weight span").removeClass("active");//移除选中的重量的样式
            });

		},
		//供应商输入提示
		loadSupplier:function(){
			  $.ajax({
		         url: "/cg-api/suppliers-dc/?is_classify=1",//提交地址
		         type: "GET",//提交方式
		         success: function (result) {
		         	
		             if (result.result == true) {
						var availableTags = [];
		             	 for (var i = 0; i < result.rows.length; i++) {
		             	 	var row=result.rows[i];
		             	 	if(row.classify.gems==true){
		             	 		var supplier_name=row.short_name?(row.short_name):(row.supplier);
		             	 		availableTags.push(supplier_name);
		             	 	}		             	 	
		             	 }
						$("#Supplier").autocomplete({
						 	 source: availableTags
						});
		             }		            		
		         }
		     });
		},
		//公共的筛选参数
		commonParamers:function(){
			var CertType = '<h4>'+_("证书")+'</h4>'
			for (var i = 0; i < gem[1].CertType.length; i++) {
					CertType += '<span data-value="'+gem[1].CertType[i][0]+'">'+ _(gem[1].CertType[i][1]) +'</span>'
			}
			$("#CertType").html(CertType);
		},
		// 切换彩宝类型
		changType:function(type){
			$("#data_process,#data_place,#data_color").remove();	
			if(type){
				//产地
				var place = '<div data-name="Place" class="term" id="data_place"><h4>'+_("产地")+'</h4>'
				for (var i = 0; i < gem[type].Place.length; i++) {
						place += '<span data-value="'+gem[type].Place[i][0]+'">'+ _(gem[type].Place[i][1]) +'</span>'
				}
				place+='</div>'
				//颜色
				var color='<div data-name="Color" id="data_color" class="term"><h4>'+_("颜色")+'</h4>'
				for (var i = 0; i < gem[type].Color.length; i++) {
					color += '<span data-value="'+gem[type].Color[i][0]+'">'+ _(gem[type].Color[i][1]) +'</span>'
				}
				color+='</div>'
				$("#Shape").after(color);//颜色
				$("#CertType").after(place);//产地
				//红宝和蓝宝
				if(type==1||type==2){
					//处理
					var process='<div data-name="Process" id="data_process" class="term"><h4>'+_("处理")+'</h4>'
					for (var i = 0; i < gem[type].Process.length; i++) {
						process += '<span data-value="'+gem[type].Process[i][0]+'">'+ _(gem[type].Process[i][1]) +'</span>'
					}  
					process+='</div>'
		            $("#data_place").after(process);//有处理
				}
			}
			
		},
		//彩宝类型对应的参数的改变
		loadTerm:function(type){
			//产地
			var place = '<h4>'+_("产地")+'</h4>';
			for (var i = 0; i < gem[type].Place.length; i++) {
					place += '<span data-value="'+gem[type].Place[i][0]+'">'+ _(gem[type].Place[i][1]) +'</span>'
			}
			//颜色
			var color='<h4>'+_("颜色")+'</h4>'
			for (var i = 0; i < gem[type].Color.length; i++) {
				color += '<span data-value="'+gem[type].Color[i][0]+'">'+ _(gem[type].Color[i][1]) +'</span>'
			}
			//红宝和蓝宝
			if(type==1||type==2){
			//处理
				var process='<div data-name="Process" id="data_process" class="term"><h4>'+_("处理")+'</h4>'
				for (var i = 0; i < gem[type].Process.length; i++) {
					process += '<span data-value="'+gem[type].Process[i][0]+'">'+ _(gem[type].Process[i][1]) +'</span>'
				}  
				process+='</div>'
				if(!($("#data_process").is(":visible"))){
	            	$("#data_place").after(process);//有处理
                }
                $(".top_container").css("height","232px");
			}else{
					$("#data_process").remove();
	                $(".top_container").css("height","208px");
				}
			$("#data_place").html(place);//产地
			$("#data_color").html(color);//颜色
		},
		
		//获取重量值
        getWeight: function ($this) {
          var weightSelected = $(".weight span.active");//选中的重量
            if (weightSelected.length == 1) {//如果只选择一个
                $("#weight-min").val(weightSelected.attr("data-value").split("-")[0]);
                $("#weight-max").val(weightSelected.attr("data-value").split("-")[1]);
            }
            else if (weightSelected.length > 1) {//选择多个
                var min = weightSelected[0];//选中的序列中的第一个的值
                var max = weightSelected[weightSelected.length - 1];//选中的序列中的最后一个的值
                $("#weight-min").val($(min).attr("data-value").split("-")[0]);//绑值
                $("#weight-max").val($(max).attr("data-value").split("-")[1]);
            } else {
                $("#weight-min").val("");
                $("#weight-max").val("");
            }
        },
		//获取参数
        getQueryParameters: function () {
           var params = {};
            params.limit = "500";
            params.useDict = true;
            isSearching = true;
            var pageSize = 1;//500
            //查询开始时间
            var dtSearchStart = new Date().getTime();
            //循环找出筛选框里选中的参数
            $(".gems-term .term").each(function (i, item) {
            		 var field = $(item).attr("data-name");
			        var dataType = $(item).attr("data-type");
			        if (field) {
			            var values = [];
			            $(item).find("span.active").each(function (j, p) {
			                var value = $(p).attr("data-value");
			                value = value ? value : $(p).text();
			               // value = dataType == 'range' ? value.split('-') : value;
			                values.push(value);
			            });
			
			            if (values.length > 0) {
			                if (dataType == 'range') {
			                    if (!params.between) {
			                        params.between = {};
			                    }
			                     if(field == "Price") { //总价
									if(!params.between.Price)
										params.between.Price = []; //初始化
									if(values.length == 1) {
										params.between.Price.push(values[0].split("-")[0]);
										params.between.Price.push(values[0].split("-")[1]);
									} else if(values.length > 1) {
										var min = values[0];
										var max = values[values.length - 1];
										params.between.Price.push(min.split("-")[0]);
										params.between.Price.push(max.split("-")[1]);
									}
								}else { //重量
									if(!params.between.Weight)
										params.between.Weight = []; //初始化    
									var min = values[0];
									var max = values[values.length - 1];
									params.between.Weight.push(min.split("-")[0]);
									params.between.Weight.push(max.split("-")[1]);
								}
			                } else {
			                    params[field] = values;
			                }
			            }
			        }              
           });
            //输入重量
		    var select_weight=$(".weight span").hasClass("active");
		    var min=$("#weight-min").val();
		    var max=$("#weight-max").val();
		    if(!select_weight && min!="" && max!=""){
		    	params.between = {};
		    	params.between.Weight=[min,max];
		    }
		    else if(!select_weight && min!="" && max==""){
		    	params.between = {};
		    	params.between.Weight=[min,99.99];
		    }
		    else if(!select_weight && min=="" && max!=""){
		    	params.between = {};
		    	params.between.Weight=[0,max];
		    }
		     else if(!select_weight && min=="" && max==""){
		    }
		    var supplier = $("#Supplier").val();
		    if (supplier!="") {
		        params.shortName = supplier;
		    }
		    var OriginSN=$("#OriginSN").val();
		    if(OriginSN!=""){
		    	params.OriginSN=OriginSN;
		    }
		    params.Category=type;			
			if(TEAM==0){
				var url_href="/Customer/Gem?team=0";//客户端的统包页面
			}else if(TEAM==1){
				var url_href="/Team/Gem?team=1";//团队端的统包页面
			}else if(TEAM==2){
				var url_href="/Team/MyGem?team=2";//团队端的现货统包页面
			}else if(TEAM==3){
				var url_href="/Team/MyGem?team=3&cid="+Customer_id;//团队端的代下单
			}
			
			var action=url_href;
		  	form = $("<form method='post' id='params_form' action='"+action+"'>"+
		   	"<input type='text' value='"+JSON.stringify(params)+"' name='params'></form>")
			$("#form_box").html(form);
			$("#params_form").submit();
        },
	}
gemsTerm.init();
})();
